package kz.simplex.photobox.action;

import kz.simplex.photobox.model.*;
import java.util.ArrayList;
import java.util.List;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.framework.EntityHome;

@Name("userHome")
public class UserHome extends EntityHome<User> {

	@In(create = true)
	RoleHome roleHome;
	@In(create = true)
	StatusHome statusHome;

	public void setUserId(Integer id) {
		setId(id);
	}

	public Integer getUserId() {
		return (Integer) getId();
	}

	@Override
	protected User createInstance() {
		User user = new User();
		return user;
	}

	public void load() {
		if (isIdDefined()) {
			wire();
		}
	}

	public void wire() {
		getInstance();
		Role role = roleHome.getDefinedInstance();
		if (role != null) {
			getInstance().setRole(role);
		}
		Status status = statusHome.getDefinedInstance();
		if (status != null) {
			getInstance().setStatus(status);
		}
	}

	public boolean isWired() {
		if (getInstance().getRole() == null)
			return false;
		if (getInstance().getStatus() == null)
			return false;
		return true;
	}

	public User getDefinedInstance() {
		return isIdDefined() ? getInstance() : null;
	}

	public List<Friendship> getFriendshipsAppliedToMe() {
		return getInstance() == null ? null : new ArrayList<Friendship>(
				getInstance().getFriendshipsAppliedToMe());
	}
	public List<Friendship> getFriendshipsIApplied() {
		return getInstance() == null ? null : new ArrayList<Friendship>(
				getInstance().getFriendshipsIApplied());
	}

}
